﻿#region Copyright information
// 
// Copyright © 2012-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the SubnetworkToolkit and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Program.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@outlook.com
// - Date Created: 2013-01-30 2:55 PM
// - Last Modified: 2013-01-30 3:21 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BOL.IO;
using CommandLine;
using SubnetworkToolkit.Data;

namespace SubnetworkToolkit.BackgroundNetwork
{
    static class Program
    {
        static void Main(string[] args)
        {
            var options = new Options();

            try
            {
                // parse arguments
                var parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
                parser.ParseArguments(args, options);

                if (args.Length == 0 || args[0] == "-h")
                    Console.Error.Write(options.GetUsage());
                else
                {
                    var pattern = options.Pattern;
                    var includeFile = options.IncludeFile;
                    var outputFile = options.OutputFile;

                    // reads include file
                    IList<int> include = null; 
                    if (!String.IsNullOrEmpty(includeFile))
                    {
                        var dtr = new DelimitedTextReader(includeFile, ",");
                        include = dtr.Read().Select(line => Int32.Parse(line[0])).ToList();
                        dtr.Close();
                    }

                    // reads network file
                    var hgr = new GeneRelationsHuman();
                    //var temp = hgr.Relations.ToList();
                    var temp = hgr.Relations.Where(x => (x.Pattern & pattern) == x.Pattern).ToList();
                    var interactions = include == null ? temp : 
                        temp.Where(x => include.Contains(x.SourceId) && include.Contains(x.TargetId)).ToList();
                    var geneIds =
                        interactions.Select(x => x.SourceId).Concat(interactions.Select(x => x.TargetId)).Distinct().
                            ToList();
                    var dictionary = geneIds.ToDictionary(x => x,
                                                          x =>
                                                          interactions.Where(y => y.SourceId.Equals(x)).Select(
                                                              y => y.TargetId).
                                                              Concat(
                                                                  interactions.Where(y => y.TargetId.Equals(x)).Select(
                                                                      y => y.SourceId)).ToList());
                    var sw = new StreamWriter(outputFile);
                    foreach (var d in dictionary)
                        sw.WriteLine("{0}\t{1}", d.Key, String.Join("\t", d.Value));
                    sw.Close();
                }
            }
            catch (Exception e)
            {
                Console.Error.Write(e.Message);
            }
        }
    }
}
